package com.buli.blmall.admin.common.response;

import com.alibaba.fastjson2.JSON;
import com.buli.blmall.admin.exception.error.LoginErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回
 * @author xiang.gao
 * @date 2024/12/5 11:54
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 等待
     */
    public static final int WAITING = 300;

    /**
     * 失败
     */
    public static final int FAILURE = 400;

    /**
     * 未登录
     */
    public static final int UNAUTHORIZED = 401;

    /**
     * 权限不足
     */
    public static final int DENIED = 403;

    /**
     * 异常
     */
    public static final int ERROR = 500;

    private int code;

    private T data;

    private String msg;

    /**
     * 创建实例
     * @param code
     * @param data
     * @param msg
     * @return
     * @param <T>
     */
    private static <T> Result<T> getInstance(int code, T data, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 无参成功返回
     * @return
     * @param <T>
     */
    public static <T> Result<T> success() {
        return getInstance(SUCCESS, null, "success");
    }

    /**
     * 有参成功返回
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        return getInstance(SUCCESS, data, "success");
    }

    /**
     *参数错误
     * @return
     */
    public static Result paramsError() {
        return getInstance(FAILURE, null, "参数错误");
    }

    /**
     *参数错误
     * @return
     */
    public static Result paramsError(String msg) {
        return getInstance(FAILURE, null, "参数错误:" + msg);
    }

    /**
     * 失败返回
     * @param msg
     * @return
     */
    public static Result failure(String msg) {
        return getInstance(FAILURE, null, msg);
    }

    /**
     * 登录失败
     * @param loginErrorCode
     * @return
     */
    public static Result loginError(LoginErrorCode loginErrorCode) {
        return getInstance(loginErrorCode.getCode(), null, loginErrorCode.getMesage());
    }

    /**
     * 失败返回
     * @param code
     * @param msg
     * @return
     */
    public static Result failure(Integer code, String msg) {
        return getInstance(code, null, msg);
    }

    /**
     *
     * @param msg
     * @return
     */
    public static Result waiting(String msg) {
        return getInstance(WAITING, null, msg);
    }

    /**
     * 未登录
     * @return
     */
    public static Result unauthorized() {
        return getInstance(UNAUTHORIZED, null, "未登录");
    }

    /**
     * 权限不足
     * @return
     */
    public static Result denied() {
        return getInstance(DENIED, null, "权限不足");
    }

    /**
     * 异常返回
     * @return
     */
    public static Result error() {
        return getInstance(ERROR, null, "系统异常");
    }

    public boolean isSuccess() {
        return SUCCESS == this.code;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
